package com.mxpio.erp.plm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.mxpio.erp.common.enums.CommonEnums;
import com.mxpio.erp.common.plm.entity.*;
import com.mxpio.erp.common.plm.service.ProjectDeliverableDraftService;
import com.mxpio.erp.common.plm.service.ProjectDeliverableService;
import com.mxpio.erp.common.plm.service.ProjectInfoDraftService;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目变更草稿表
 */

/**
 * 项目变更草稿表
 */
@Service
public class ProjectInfoDraftServiceImpl extends BaseServiceImpl<ProjectInfoDraft> implements ProjectInfoDraftService {
    /**
     * 保存项目草稿
     * @param projectInfo
     */
    @Autowired
    private ProjectDeliverableService projectDeliverableService;
    @Autowired
    private ProjectDeliverableDraftService projectDeliverableDraftService;
    @Override
    @Transactional
    public void saveDraft(ProjectInfo projectInfo, ProjectInfoChange projectInfoChange)  {
        ProjectInfoDraft projectInfoDraft = new ProjectInfoDraft();
        projectInfoDraft.setInfoDeptId(projectInfo.getInfoDeptId());
        projectInfoDraft.setBizNo(projectInfoChange.getBizNo());
        projectInfoDraft.setProCode(projectInfo.getProCode());
        projectInfoDraft.setProName(projectInfo.getProName());
        projectInfoDraft.setStatus(projectInfo.getStatus());
        projectInfoDraft.setSchedule(projectInfo.getSchedule());
        projectInfoDraft.setProStatus(projectInfo.getProStatus());
        projectInfoDraft.setPriority(projectInfo.getPriority());
        projectInfoDraft.setProSource(projectInfo.getProSource());
        projectInfoDraft.setProType(projectInfo.getProType());
        projectInfoDraft.setProLeader(projectInfo.getProLeader());
        projectInfoDraft.setProSecretary(projectInfo.getProSecretary());
        projectInfoDraft.setPlanStartTime(projectInfo.getPlanStartTime());
        projectInfoDraft.setPlanEndTime(projectInfo.getPlanEndTime());
        projectInfoDraft.setDuration(projectInfo.getDuration());
        projectInfoDraft.setProBudget(projectInfo.getProBudget());
        projectInfoDraft.setProRange(projectInfo.getProRange());
        projectInfoDraft.setProTarget(projectInfo.getProTarget());
        projectInfoDraft.setRules(projectInfo.getRules());
        projectInfoDraft.setIsTemplate(projectInfo.getIsTemplate());
        projectInfoDraft.setIsAuditEnd(CommonEnums.YesNo.NO.getCode());
        projectInfoDraft.setChangeType(projectInfoChange.getChangeType());
        projectInfoDraft.setReason(projectInfoChange.getReason());
        this.save(projectInfoDraft);
//        //修改的交付物
//        List<ProjectDeliverable> projectDeliverable = projectInfo.getProjectDeliverable();
//        Criteria criteria = Criteria.create().addCriterion("proCode", Operator.EQ, projectInfo.getProCode());
//        List<ProjectDeliverable> list = projectDeliverableService.list(ProjectDeliverable.class, criteria);//取出本身的交付物
//        if (projectDeliverable.size() > 0 && list.size() > 0) {
//            List<String> updateDeliverables = projectDeliverable.stream()
//                    .map(ProjectDeliverable::getDocNO) // 假设ProjectDeliverable类有一个getDocNO方法返回docNO属性
//                    .collect(Collectors.toList());
//
//            List<String> deliverables = list.stream()
//                    .map(ProjectDeliverable::getDocNO) // 假设ProjectDeliverable类有一个getDocNO方法返回docNO属性
//                    .collect(Collectors.toList());
//            List<String> differentDeliverables = deliverables.stream()
//                    .filter(docNO -> !updateDeliverables.contains(docNO))
//                    .collect(Collectors.toList());
//            for (String docNO : differentDeliverables) {
//                ProjectDeliverable one = JpaUtil.linq(ProjectDeliverable.class).equal("docNO", docNO).findOne();
//                ProjectDeliverableDraft draft = new ProjectDeliverableDraft();
//                //此交付物没有修改，存入草稿表
//                BeanUtil.copyProperties(one, draft, "id", "proCode", "crudType");
//                draft.setProCode(projectInfo.getProCode());
//                projectDeliverableDraftService.save(draft);
//            }
//        }

//        List<ProjectDeliverableDraft> projectDeliverableDraft = new ArrayList<>();
//        //交付物，草稿
//        List<ProjectDeliverableDraft> projectDeliverableDraft = projectInfoDraft.getProjectDeliverableDraft();
//        if (projectDeliverable != null && projectDeliverable.size() > 0) {//当有修改的数据时
//            for (ProjectDeliverable deliverable : projectDeliverable) {
//                ProjectDeliverableDraft draft = new ProjectDeliverableDraft();
////                //将没有进行修改的交付物遍历出来
////                for (ProjectDeliverable deliverable1 : list) {
////                    if (!deliverable.getDocNO().equals(deliverable1.getDocNO())) {
////                        //此交付物没有修改，存入草稿表
////                        BeanUtil.copyProperties(deliverable1, draft, "id", "proCode", "crudType");
////                        draft.setProCode(projectInfo.getProCode());
////                        projectDeliverableDraftService.save(draft);
////                    }
////                }
//                if (!deliverable.getCrudType().name().equals("DELETE")) {
//                    BeanUtil.copyProperties(deliverable, draft, "id", "proCode", "crudType");
//                    draft.setProCode(projectInfo.getProCode());
//                    projectDeliverableDraftService.save(draft);
//                }
//            }
//        }else for (ProjectDeliverable deliverable1 : list) {
//            ProjectDeliverableDraft draft = new ProjectDeliverableDraft();
//            //此交付物没有修改，存入草稿表
//            BeanUtil.copyProperties(deliverable1, draft, "id", "proCode", "crudType");
//            draft.setProCode(projectInfo.getProCode());
//            projectDeliverableDraftService.save(draft);
//
//        }


//        projectInfoDraft.setProjectDeliverableDraft(projectDeliverableDraft);
//        this.save(projectInfoDraft);
//        this.save(projectInfoDraft,new SmartCrudPolicyAdapter(){
//            @Override
//            public boolean beforeInsert(CrudContext context) {
//                Object o = context.getEntity();
//                if (o instanceof ProjectDeliverableDraft){
//                    ProjectInfoDraft infoDraft= context.getParent();
//                    ((ProjectDeliverableDraft)o).setProCode(infoDraft.getProCode());
//                }
//                return true;
//            }
//        });

    }

}
